package com.lupus.servlet;
import java.io.IOException;
import java.util.*;
import java.util.logging.*;
import javax.servlet.http.*;
import com.lupus.business.Business;
import com.lupus.common.*;
import com.lupus.util.*;

public final class ServiceServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	static Logger logger = Logger.getLogger(ServiceServlet.class.getName());
	
	public void doPost(HttpServletRequest request,HttpServletResponse response){
		Map<String,Object> currentUser  = (Map<String,Object>)request.getSession().getAttribute("currentUser");
		Map<String,Object> responseMap = new HashMap<String,Object>();
		try{
			String requestString = StreamUtil.getContent(request).replaceAll("<xml>","").replaceAll("</xml>","").replaceAll("#localLanguage#",request.getLocale().getLanguage());
			requestString = requestString.replaceAll("#currentUser#",currentUser==null?"":(String)currentUser.get("OID")).replaceAll("#currentTime#",DateUtil.getCurrentTime()).replaceAll("#currentIdentity#",IdentityUtil.getCurrentIdentity());
			Map<String,Object> requestMap = ConvertFactory.stringToMap(requestString);
			responseMap = Business.getConcreteBusiness(requestMap).perform(requestMap,request,response);
			response.getWriter().write(ConvertFactory.mapToString(responseMap));
		}catch(Exception e){
			responseMap.put("returnFlag",VariableFactory.FAIL);
			responseMap.put("exceptionMessage",ExceptionUtil.getExceptionString(e));
			responseMap.put("returnTip","common.tip.systemerror");
			try {
				response.getWriter().write(ConvertFactory.mapToString(responseMap));
			} catch (IOException e1) {
				logger.log(Level.WARNING,"",e1);
			}
			logger.log(Level.WARNING,"",e);
		}
	}
}